package com.g9.blog.controller;

import com.g9.blog.common.Response;
import com.g9.blog.common.Result;
import com.g9.blog.entity.Comment;
import com.g9.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: Graycat
 * @Date: 2020/9/12 08:52
 * Create by lwj.
 */

@RestController
@Transactional
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Value("${lalala.save-path}") String param;

    @GetMapping("/test")
    public void test(  ){
        System.out.println(param);
    }

    // 给评论点赞
    @PostMapping("/comment/like")
    public Result giveCommentLike(  int commentId ){
        commentService.addCommentLikeCount(commentId);
        return Response.makeOKRsp();
    }

    // 建立(发表)评论
    @PostMapping("/comment")
    public Result addOrUpdateComment( Comment comment ){
        // 文章评论数+1，
        commentService.addArticleCommentCount(comment);
        commentService.save(comment);
        return Response.makeOKRsp();
    }

    // 按id删除评论
    @DeleteMapping("/comment/{commentId}")
    public Result deleteCommentById( @PathVariable int commentId ){
        commentService.deleteById(commentId);
        return Response.makeOKRsp();
    }

    // 根据commentid返回评论
    @GetMapping("/comment/{commentId}")
    public Result getCommentById( @PathVariable int commentId ){
        return Response.makeOKRsp(commentService.findById(commentId));
    }

    // 根据文章id返回该文章得所有评论
    @GetMapping("/comment/byarticleid/{articleId}")
    public Result getCommentByArticleId( @PathVariable int articleId ){
        List<Comment> articleList = commentService.getCommentByArticleId(articleId);
        return Response.makeOKRsp(articleList);
    }


}
